//
//  YWSubscriptionTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/10.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWSubscriptionTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWEquipmentType.h"

@interface YWSubscriptionTableViewCell(){
    BOOL isSelectSystem;
}
@property (nonatomic, strong) UIButton *selectButton;
@end

@implementation YWSubscriptionTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (UIButton *)newSelectButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"+" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    button.layer.borderColor = [UIColor customTextGrayColor].CGColor;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 15.0;
    [button addTarget:self action:@selector(handleSelectButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UIButton *)newSelectNameButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = kContentFont;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor customTextGrayColor].CGColor;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 15.0;
    [button addTarget:self action:@selector(handleCancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)setUpSubviews{
    _selectButton = [self newSelectButton];
    _selectButton.frame = CGRectMake(10, 10, KNameLabelWidth, KNameLabelHeight);
    [self.contentView addSubview:_selectButton];
}

-(void)setBtnTag:(NSInteger)btnTag{
    _btnTag = btnTag;
    _selectButton.tag = btnTag;
}

- (void)setSystemTypeArray:(NSMutableArray *)systemTypeArray{
    _systemTypeArray = systemTypeArray;
    isSelectSystem = YES;
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat w = 0;
    __block CGFloat h = 10;
    [_systemTypeArray enumerateObjectsUsingBlock:^(YWEquipmentSystemType*  _Nonnull systemType, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * button = [self newSelectNameButton];
        CGSize size =[systemType.systemTypeName sizeWithAttributes:@{NSFontAttributeName:kContentFont}];
        CGFloat length = size.width;
        button.frame = CGRectMake(10 + w, h, length + 15 , 30);
        if(10 + w + length + 15 + 10 > SCREEN_WIDTH){
            w = 0; //换行时将w置为0
            h = h + button.frame.size.height + 10;//距离父视图也变化
            button.frame = CGRectMake(10 + w, h, length + 20, 30);//重设button的frame
        }
        w = CGRectGetMaxX(button.frame);
        button.tag = 1000 + idx;
        [button setTitle:systemType.systemTypeName forState:UIControlStateNormal];
        [self.contentView addSubview:button];
    }];
    if (10+w+KNameLabelWidth + 10 > SCREEN_WIDTH) {
        w = 0;
        self.selectButton.frame = CGRectMake(10+w, h+KNameLabelHeight+10, KNameLabelWidth, KNameLabelHeight);
    }else{
        self.selectButton.frame = CGRectMake(10+w, h, KNameLabelWidth, KNameLabelHeight);
    }
    [self.contentView addSubview:self.selectButton];
}

- (void)setEquipmentTypeArray:(NSMutableArray *)equipmentTypeArray{
    _equipmentTypeArray = equipmentTypeArray;
    isSelectSystem = NO;
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat w = 0;
    __block CGFloat h = 10;
    [_equipmentTypeArray enumerateObjectsUsingBlock:^(YWEquipmentType*  _Nonnull equipmentType, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * button = [self newSelectNameButton];
        CGSize size =[equipmentType.equipmentTypeName sizeWithAttributes:@{NSFontAttributeName:kContentFont}];
        CGFloat length = size.width;
        button.frame = CGRectMake(10 + w, h, length + 15 , 30);
        if(10 + w + length + 15 + 10 > SCREEN_WIDTH){
            w = 0; //换行时将w置为0
            h = h + button.frame.size.height + 10;//距离父视图也变化
            button.frame = CGRectMake(10 + w, h, length + 20, 30);//重设button的frame
        }
        w = CGRectGetMaxX(button.frame);
        button.tag = 1000 + idx;
        [button setTitle:equipmentType.equipmentTypeName forState:UIControlStateNormal];
        [self.contentView addSubview:button];
    }];
    if (10+w+KNameLabelWidth + 10 > SCREEN_WIDTH) {
        w = 0;
        self.selectButton.frame = CGRectMake(10+w, h+KNameLabelHeight+10, KNameLabelWidth, KNameLabelHeight);
    }else{
        self.selectButton.frame = CGRectMake(10+w, h, KNameLabelWidth, KNameLabelHeight);
    }
    [self.contentView addSubview:self.selectButton];
}

- (void)handleSelectButtonEvent:(UIButton*)sender{
    NSInteger tag = sender.tag;
    if (tag == KSelectSystemTypeBtnTag) {
        if ([self.delegate respondsToSelector:@selector(jumpToSelectType:)]) {
            [self.delegate jumpToSelectType:tag];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(jumpToSelectType:)]) {
            [self.delegate jumpToSelectType:tag];
        }
    }
}

- (void)handleCancelEvent:(UIButton*)sender{
    NSInteger tag = sender.tag-1000;
    if (isSelectSystem) {
        YWEquipmentSystemType * systemType = [self.systemTypeArray objectAtIndex:tag];
        [self.systemTypeArray removeObjectAtIndex:tag];
        if ([self.delegate respondsToSelector:@selector(cancelButtonEvent:withSystemType:)]) {
            [self.delegate cancelButtonEvent:tag withSystemType:systemType];
        }
    }else{
        [self.equipmentTypeArray removeObjectAtIndex:tag];
        if ([self.delegate respondsToSelector:@selector(cancelBtnEventWithEquType:)]) {
            [self.delegate cancelBtnEventWithEquType:tag];
        }
    }
}

@end
